package controller.student.info;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import dao.Student;

@WebServlet("/student/info/edit")
public class edit extends controller.student.Base {
    private static final long serialVersionUID = 1L;

    public edit() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        view.display("student/form.jsp");
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        long id = (Long) request.getAttribute("id");
        try {
            PreparedStatement stmt = Student.update("`id`=" + id, "name");
            stmt.setString(1, name);
            stmt.executeUpdate();
            Student.Data d;
            d = Student.findById((long) request.getAttribute("id"));
            HttpSession session = request.getSession();
            session.setAttribute("user-type", "student");
            session.setAttribute("user-data", d);
            view.sendRedirect("index");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
